﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //方法一
            var conString = "server=.;database=sqltext;uid=sa;pwd=newpassword";
            SqlConnection sqlConnection = new SqlConnection(conString);
            sqlConnection.Open();
            var create = " select * from table";
            var input = "insert into newtext ('Name') values ('马超')";//添加
            var delete = "delete from newtext  where Id=3";//删除
            var update = "update newtext set Name='狼' where Id=2;";//更新
            SqlCommand sql = new SqlCommand(create, sqlConnection);
            sql = new SqlCommand(input, sqlConnection);
            sql = new SqlCommand(delete, sqlConnection);
            sql = new SqlCommand(update, sqlConnection);
            var reader = sql.ExecuteReader();
            Console.WriteLine("{0},{1}",reader["Id"],reader["Name"]);
            while (reader.Read())
            {
                Console.WriteLine("{0},{1}",reader[0],reader[1] );
            }
            //方法二
            DataTable dataTable = new DataTable();
            
            SqlDataAdapter sqlData = new SqlDataAdapter(create, sqlConnection);
            sqlData.Fill(dataTable);
            foreach (DataRow item in dataTable.Rows)
            {
                Console.WriteLine("{0}\t{1}\t\t{2}\t{3}", item[0], item[1], item[2], item[3]);
            }
            reader.Close();
        }
    }
}
